package com.xiaomi.controller;

import com.alibaba.fastjson.JSON;
import com.xiaomi.entity.Pingjia;
import com.xiaomi.model.Result;
import com.xiaomi.service.PingjiaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName PingjiaController
 * @Description TODO
 * @Author zcy
 * @Date 2021/12/1 0:16
 * @Version 1.0
 **/
@Controller
@RequestMapping(value = "/pingjia",produces = "application/json;charset=utf-8")
public class PingjiaController {
    @Autowired
    private PingjiaService pingjiaService;
    @GetMapping("/list.do")
    @ResponseBody
    public String select(@RequestParam Integer page, @RequestParam Integer limit,
                          @RequestParam(required = false) Integer pjTongguo,
                         @RequestParam(required = false) String name){
        Map<String,Object> pars = new HashMap<>();
        pars.put("pjTongguo",pjTongguo);
        pars.put("name",name);
        if (page!=null && limit!=null){
            pars.put("offset",(page-1)*limit);
            pars.put("limit",limit);
        }


        List<Pingjia> list = pingjiaService.pageSearch(pars);
        Integer count = pingjiaService.getCount(pars);

        Result result = new Result();
        result.setData(list);
        result.setCode(0);
        result.setCount(count);
        String json = JSON.toJSONString(result);
        return json;
    }
}
